package com.huawei.esdk.tp.device.smc;

import java.util.List;
import java.util.Map;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.constants.ESDKConstant;
import com.huawei.esdk.platform.common.constants.ESDKErrorCodeConstant;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.commu.itf.IProtocolAdapterManager;
import com.huawei.esdk.platform.nemgr.itf.IDeviceManager;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.common.constants.TPConstant;
import com.huawei.esdk.tp.device.aa.SMCAAService;
import com.huawei.esdk.tp.domain.model.bean.AuthorizeInfo;
import com.huawei.esdk.tp.southcommu.TPCXFSOAPCustProvider;
import com.huawei.esdk.tp.southcommu.cxf.gen.KeepAliveResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.LoginRequestResponseSouth;

public class CommonCapabilityImpl implements ICommonCapability
{
    private SMCAAService smcAAService;
    
    private IDeviceManager deviceManager;
    
    private IProtocolAdapterManager protocolAdapterManager;
    
    private boolean initialized = false;
    
    public void init()
    {
    }
    
    private void setup()
    {
        ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter =
            (ICXFSOAPProtocolAdapter)protocolAdapterManager.getProtocolInstanceByType(ESDKConstant.PROTOCOL_ADAPTER_TYPE_SOAP_CXF,
                getSap());
        cxfSOAPProtocolAdapter.setCXFSOAPCustProvider(new TPCXFSOAPCustProvider());
        smcAAService = new SMCAAService(cxfSOAPProtocolAdapter);
    }
    
    private String getSap()
    {
        List<Map<String, Object>> devices = deviceManager.queryDeviceInfo(TPConstant.DEV_ID_SMC);
        return (String) devices.get(0).get("sap");
    }
    
    @Override
    public SDKResult<AuthorizeInfo> loginRequest(String userName)
    {
        if (!initialized)
        {
            setup();
        }
        SDKResult<AuthorizeInfo> result = new SDKResult<AuthorizeInfo>();
        AuthorizeInfo authorizeInfo = new AuthorizeInfo();
        result.setResult(authorizeInfo);
        
        LoginRequestResponseSouth loginRes = smcAAService.smcLoginRequest(userName);
        if (null == loginRes)
        {
            result.setErrCode(ESDKErrorCodeConstant.ERROR_CODE_SDK_AUTHORIZE_FAILURE);
        }
        //DTS2015011901913 修改
        else if (0 != loginRes.getLoginRequestResult().getErrCode())
        {
            result.setErrCode(loginRes.getLoginRequestResult().getErrCode());
        }
        else
        {
            result.setErrCode(loginRes.getLoginRequestResult().getErrCode());
            authorizeInfo.setHashType(loginRes.getHashType().toString());
            authorizeInfo.setRandomSequence(loginRes.getRandomSequence());
            authorizeInfo.setSessionId((String)ThreadLocalHolder.get()
                .getEntities()
                .get(InfoKeys.SMC_SESSION_ID.toString()));
        }
        
        return result;
    }
    
    public IProtocolAdapterManager getProtocolAdapterManager()
    {
        return protocolAdapterManager;
    }
    
    public void setProtocolAdapterManager(IProtocolAdapterManager protocolAdapterManager)
    {
        this.protocolAdapterManager = protocolAdapterManager;
    }

    public IDeviceManager getDeviceManager()
    {
        return deviceManager;
    }

    public void setDeviceManager(IDeviceManager deviceManager)
    {
        this.deviceManager = deviceManager;
    }

    @Override
    public SDKErrorCode keepAlive()
    {
        SDKErrorCode result = new SDKErrorCode();
        KeepAliveResponseSouth keepAlive= smcAAService.smcKeepAlive();
        if (null != keepAlive.getKeepAliveResult())
        {
            result.setErrCode(keepAlive.getKeepAliveResult().getErrCode());
        }
        else
        {
            result.setErrCode(ErrInfo.SESSIONID_ERRORCODE);
        }
        return result;
    }
}
